//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by 美食家 on 17/3/28.
//  Copyright © 2017年 不孤独的美食家. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        setAddtions()
       
        window=UIWindow()
        window?.backgroundColor=UIColor.white
        window?.rootViewController=LHTabBarController()
        window?.makeKeyAndVisible()
        loadJson()
        return true
    }

}

extension AppDelegate{

    func setAddtions() -> Void {
        
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        AFNetworkActivityIndicatorManager.shared().isEnabled=true
        
        
        /** 取得用户授权显示通知 */
        if #available(iOS 10.0, *) {
            // iOS 10
            let center = UNUserNotificationCenter.current()
            center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, error) in
                if error != nil {
                    return
                }
                
            })
            
        } else {
            // iOS 9
            let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
        }

        
    }

}

extension AppDelegate{

    func loadJson() -> Void{
    
    let url=Bundle.main.url(forResource: "main.json", withExtension: nil)
    let data = NSData(contentsOf: url!)
    let docDir=NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let pathJson=(docDir as NSString).appendingPathComponent("main.json")
    data?.write(toFile: pathJson, atomically: true)
//      print("\(pathJson)")
    }
}

